/*
 * Wenjiankongzhiimpl1.h
 *
 *  Created on: 2022年7月13日
 *      Author: wocao
 */
#include <cstdio>
#include <fstream>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <iostream>
#include <uuid/uuid.h>
#include "../../Wenjian_quanju.h"
#include "../../wenjian_impl/Wenjianimpl1.h"
#include "../../../Serializable/Serializable.hpp"
#include "../../../cunchuti/kongzhi/Kongzhi.h"
#include "../Wenjiankongzhi.h"
using namespace std;
#ifndef WENJIANCENG_KONGZHI_KONGZHI_IMPL_WENJIANKONGZHIIMPL1_H_
#define WENJIANCENG_KONGZHI_KONGZHI_IMPL_WENJIANKONGZHIIMPL1_H_

class Wenjian_kongzhi_impl1: public Wenjian_kongzhi {
public:
	virtual ~Wenjian_kongzhi_impl1();
	Wenjian_kongzhi_impl1(Kongzhi *pkongzhi);
	void add_file(RW_status_cache &rwcache, Wenjian *wenjian) override;
	long get_file(RW_status_cache &rwcache, const string &file_name) override;
	void rem_file(const string &file_name) override;
	void create_dir(const string &new_dir) override;
	void dir_zhuanhuan(const string &dir) override;
	void dir_zhuanhuan_gen() override;
	void dir_zhuanhuan_xiangdui(const string &dir) override;
	void dir_zhuanhuan_juedui(const string &dir) override;
	long get_kuai_size(long size) override;
	void replace_dir() override;
	void add_file_dir(Wenjian *wenjian) override;
	void red_file_dir(const string &file_name) override;
	void read_dir(const string dir);
	void re_now_dir(Wenjian *pwenjian, unsigned long size);
	void filename_to_addname(const string &new_dir, string &add_name);
	vector<unsigned long>* get_file_addlist(const string &file_name);
	unsigned long bianli_dir(char **buf) {
		map<string, Wenjian>::iterator it = WENJIANKONGZHI_DIRLIST.begin();
		vector<char> *vct = new vector<char>();
		vct->push_back('[');
		while (1) {
			const char *bfn = it->second.get_file_name();
			vct->push_back('{');
			string name("\"name\"");
			const char *pname = name.data();
			for (unsigned long i = 0; i < name.size(); ++i) {
				vct->push_back(pname[i]);
			}
			vct->push_back(':');
			vct->push_back('"');
			for(int i=0;i<255;++i){
				if(bfn[i]!='\0'){
					vct->push_back(bfn[i]);
				}else{
					break;
				}
			}
			vct->push_back('"');
			vct->push_back(',');
//			copy(bfn,bfn+255,vct->begin());
			unsigned long file_size = it->second.get_file_size();
			string sfz = to_string(file_size);
			string size("\"size\"");
			const char *psize = size.data();
			for (unsigned long i = 0; i < size.size(); ++i) {
				vct->push_back(psize[i]);
			}
			vct->push_back(':');
			vct->push_back('"');
			for(unsigned long i = 0;i<sfz.size();++i){
				vct->push_back(sfz[i]);
			}
			vct->push_back('"');
			vct->push_back(',');
//			copy(sfz.data(),sfz.data()+8,vct->begin()+255);
			string leixing("\"leixing\"");
			const char *pl = leixing.data();
			for(unsigned long i = 0;i<leixing.size();++i){
				vct->push_back(pl[i]);
			}
			vct->push_back(':');
			vct->push_back('"');
			char lei = it->second.get_file_leixing();
			if(lei == 0){
				vct->push_back('0');
			}else{
				vct->push_back('1');
			}
			vct->push_back('"');
			vct->push_back('}');
//			cout << it->first << "     ";
//			cout << (it->second.get_file_name()) << " "
//					<< (it->second.get_file_add()) << " "
//					<< (it->second.get_file_size()) << endl;
			++it;
			if(it != WENJIANKONGZHI_DIRLIST.end()){
				vct->push_back(',');
			}else{
				break;
			}
		}
		vct->push_back(']');
		unsigned long vsize = vct->size();
		char *bbuf = new char[vsize];
//		for(unsigned long i = 0;i<vsize;++i){
//			bbuf[i] = (*vct)[i];
//		}
//		cout<<vsize<<endl;
		copy(vct->begin(),vct->end(),bbuf);
//		for (unsigned long i = 0; i < vsize; ++i) {
//			cout << bbuf[i];
//		}
//		cout << endl;
		*buf = bbuf;
		delete vct;
		return vsize;
	}
};

#endif /* WENJIANCENG_KONGZHI_KONGZHI_IMPL_WENJIANKONGZHIIMPL1_H_ */
